<?php

namespace common\models\project;

use common\helpers\StringHelper;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_item".
 *
 * @property int $id
 * @property int $merchant_id 商户id
 * @property int $region_id 地区表id
 * @property string $title 标题
 * @property string $content 内容
 * @property string $image_project 项目图片
 * @property int $project_hits 点击数
 * @property string $xmdz 项目地址
 * @property string $slcdz 售楼处地址
 * @property int $dtxmzt 地图项目状态 /3.在建/2待售/1.在售/0.已售
 * @property string $ckjg 参考价格
 * @property string $zlhx 主力户型
 * @property string $kpsj 开盘时间
 * @property string $jfsj 交房时间
 * @property string $lxdh 联系电话
 * @property string $yjbh 一键拨号
 * @property string $ytlx 业态类型
 * @property string $xmzt 项目状态
 * @property string $zxzt 装修状态
 * @property string $keywords 项目关键词
 * @property double $ptzbx 配套坐标经度
 * @property double $ptzby 配套坐标纬度
 * @property string $snqy 市内区域
 * @property string $cqnx 产权年限
 * @property string $dbby 顶部标语
 * @property string $wylb 物业类别
 * @property string $jzlb 建筑类别
 * @property string $zdmj 占地面积
 * @property string $jzmj 建筑面积
 * @property string $zhs 总户数
 * @property string $cwpb 车位配比
 * @property string $rjl 容积率
 * @property string $lhl 绿化率
 * @property string $wygs 物业公司
 * @property string $lczk 楼层状况
 * @property string $hxqj 户型区间
 * @property string $khtj 猜你喜欢
 * @property string $bz 备注
 * @property double $slczbx 售楼处坐标经度
 * @property double $slczby 售楼处坐标纬度
 * @property string $wyglf 物业管理费
 * @property int $yjtj 佣金推荐
 * @property int $zyjby 赚佣金额度
 *
 * @property int $xqyfxhb 项目分享海报
 * @property int $gwfxhb 顾问分享海报
 *
 * @property int $issend 是否支持投稿
 *
 * @property string $yjby 赚佣金标语
 * @property int $dy_hits 抖音小程序点击数
 * @property int $sort 排序
 * @property int $created_at 创建时间
 * @property int $updated_at 最后一次更新时间
 * @property int $status 状态[-1删除；0禁用；1启用]
 */
class Project extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [[ 'title','region_id'], 'required'],
            [['merchant_id', 'region_id', 'project_hits', 'yjtj', 'dy_hits', 'sort', 'created_at', 'updated_at', 'status'], 'integer'],
            [['ptzbx', 'dtxmzt', 'ptzby','khtj', 'slczbx', 'slczby','issend'], 'number'],
            [['image_project','xqyfxhb','gwfxhb', 'keywords'], 'string', 'max' => 255],
            [['title','wygs','zyjby'], 'string', 'max' => 63],
            [['xmdz', 'slcdz', 'snqy', 'bz'], 'string', 'max' => 127],
            [['ckjg', 'zlhx'], 'string', 'max' => 63],
            [['lxdh', 'yjbh', 'ytlx', 'xmzt', 'zxzt', 'cqnx', 'wylb', 'jzlb', 'zdmj', 'jzmj', 'zhs', 'cwpb', 'rjl', 'lhl', 'lczk', 'hxqj', 'wyglf'], 'string', 'max' => 63],
            [['yjby','dbby'], 'string', 'max' => 63],
            [['content'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '项目ID',
            'title' => '标题',
            'image_project' => '项目缩略图',
            'merchant_id' => '商户 ID',
            'region_id' => '地区选择',
            'content' => '内容',
            'project_hits' => '访问次数',
            'xmdz' => '项目地址',
            'ptzbx' => '项目坐标经度',
            'ptzby' => '项目坐标纬度',
            'slcdz' => '售楼处地址',
            'slczbx' => '售楼处坐标经度',
            'slczby' => '售楼处坐标纬度',
            'dtxmzt' => '地图项目状态',
            'ckjg' => '参考价格',
            'zlhx' => '主力户型',
            'kpsj' => '开盘时间',
            'jfsj' => '交房时间',
            'lxdh' => '联系电话',
            'yjbh' => '一键拨号',
            'ytlx' => '业态类型',
            'xmzt' => '项目状态',
            'zxzt' => '装修状态',
            'keywords' => '项目关键词',
            'snqy' => '市内区域（详细地址）',
            'dbby' => '顶部标语',
            'cqnx' => '产权年限',
            'wylb' => '物业类别',
            'jzlb' => '建筑类别',
            'zdmj' => '占地面积',
            'jzmj' => '建筑面积',
            'zhs' => '总户数',
            'cwpb' => '车位配比',
            'rjl' => '容积率',
            'lhl' => '绿化率',
            'lczk' => '楼层状况',
            'hxqj' => '户型区间',
            'bz' => '备注',
            'wygs' => '物业公司',
            'wyglf' => '物业管理费',
            'khtj'=> '猜你喜欢',
            'yjtj' => '佣金推荐',
            'zyjby' => '赚佣金额度',
            'yjby' => '赚佣金标语',
            'xqyfxhb' => '项目分享海报',
            'gwfxhb' => '顾问分享海报',
            'dy_hits' => '抖音小程序点击数',
            'issend' => '是否支持投稿',
            'sort' => '排序',
            'created_at' => '创建时间',
            'updated_at' => '最后一次更新时间',
            'status' => '状态',
        ];
    }
    public function getRegion()
    {
        return $this->hasOne(ProjectRegion::className(), ['id' => 'region_id']);
    }


    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }
        $this->kpsj = StringHelper::dateToInt($this->kpsj." 00:00:00");
        $this->jfsj = StringHelper::dateToInt($this->jfsj." 00:00:00");

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
